package com.hospital.BaseActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.*;
import android.widget.*;
import com.hospital.Common.HttpAsyncXmlCallBack;
import com.hospital.Common.NetUtil;
import com.hospital.activity.R;
import com.hospital.adapter.HospWheelAdapter;
import com.hospital.tools.HashMapMader;
import com.hospital.tools.MyGestureDetector;
import com.hospital.xml.XMLMaker;
import org.json.JSONObject;
import wheel.WheelView;

import java.util.*;

/**
 * Created by wangjun on 2015/6/2.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {

        private EditText edit_phone, edit_pwd, edit_code, edit_pwd2;
        private Button getCode;
        private TextView tv_name,yhxy;
        private RelativeLayout school_chose;

        private CheckBox agree;
        private SharedPreferences sp;
        int schoolId = 0;
        private  boolean getCodefalg=true;
        private ProgressDialog pd;
        private ScrollView scrollView1;
        MyGestureDetector myGestureDetector = null;
        GestureDetector gestureDetector = null;



    //?????????
    private WheelView hosptis;
       private HospWheelAdapter hospWheelAdapter;
    private TextView wheelCancel;
    private TextView wheelSure;
    private RelativeLayout region_layout;
    private List<HashMap<String, String>> hosList=new ArrayList<HashMap<String, String>>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.register_people);
            mContext=this;
            mPageName="??????";
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                    WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
   // SimpleAdapter
            sp = getSharedPreferences("user_login", 0);

            yhxy=(TextView)findViewById(R.id.yhxy);
            yhxy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
               //     startActivity(new Intent(Reg.this, UserXyActivity.class));
                }
            });
            initView();
            scrollView1 = (ScrollView) findViewById(R.id.scrollView1);
            scrollView1.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    return gestureDetector.onTouchEvent(motionEvent);
                }
            });
            //Util.softInputTouchListener(this,scrollView1);
            gestureDetector = new GestureDetector(this, new MyGestureDetector(this,scrollView1));

        }

        private void initView() {
            edit_phone = (EditText) findViewById(R.id.register_company_username);
            edit_pwd = (EditText) findViewById(R.id.register_company_pwd);
            edit_code = (EditText) findViewById(R.id.register_company_code);
            edit_pwd2 = (EditText) findViewById(R.id.register_company_pwd2);
            tv_name = (TextView) findViewById(R.id.register_company_name);
            getCode = (Button) findViewById(R.id.register_request_code);
            school_chose = (RelativeLayout) findViewById(R.id.register_user_choseSchool);

            agree = (CheckBox) findViewById(R.id.agree);
            getCode.setOnClickListener(this);
            school_chose.setOnClickListener(this);



           //滚轮控件
            region_layout = (RelativeLayout) findViewById(R.id.region_layout);
            wheelCancel = (TextView) findViewById(R.id.wheel_cancel);
            wheelSure = (TextView) findViewById(R.id.wheel_sure);
            hosptis = (WheelView) findViewById(R.id.region);
            hosptis.setWheelBackground(R.drawable.wheel_bg_region);
            hosptis.setWheelForeground(R.drawable.wheel_val_region);
            hosptis.setDrawShadows(false);
            wheelSure.setOnClickListener(this);
            wheelCancel.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {

                case R.id.register_request_code:
                    getCode();
                    break;
                case R.id.register_user_choseSchool:
                    getHos();
                    break;
                case R.id.wheel_cancel:

                    region_layout.setVisibility(View.GONE);
                    break;

                case R.id.wheel_sure:
                    Toast.makeText(this,hosList.get(hosptis.getCurrentItem()).get("hospital_name"),Toast.LENGTH_LONG).show();
                    region_layout.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        }

        int i = 60;


    private void getHos(){

        String xml = new XMLMaker("1", "1").getHospitalList("","Y");

        if (hospWheelAdapter != null) {
            hosptis.setViewAdapter(hospWheelAdapter);
            region_layout.setVisibility(View.VISIBLE);
            return;
        }
        NetUtil.SoapWsProcessRunnable(xml,new HashMapMader().getHospitalList(),new HttpAsyncXmlCallBack(){
            @Override
            public void onSuccess(List<HashMap<String, String>> hosList1) {
              //  if (mPd != null) mPd.dismiss();
                if (hosList1.size() < 1) {

                    Message msg = new Message();


                    msg.arg1=-1;
                    //     }
                    handler.sendMessage(msg);

                }else{
                    hosList=hosList1;
                    Message msg = new Message();
                    msg.obj=hosList;
                    msg.arg1=1;
                    handler.sendMessage(msg);
                }



            }




        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.arg1) {
                case -1:
                    LayoutInflater inflater = getLayoutInflater();
                    //??????XML?????????ID???????????????
                    View layout = inflater.inflate(R.layout.shibai, null);

                    ImageView image = (ImageView) layout.findViewById(R.id.shibaiimga);
                    //???ò??????????????
                    image.setImageResource(R.drawable.shibai);

                    TextView text = (TextView) layout.findViewById(R.id.shibaitext);
                    //????????
                    text.setText("???????");

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_SHORT);
                    toast.setView(layout);
                    toast.show();
                    break;

                case 1:
                    List<HashMap<String, String>> obj = (List<HashMap<String, String>>) msg.obj;
                    try {

                        hospWheelAdapter = new HospWheelAdapter(RegisterActivity.this, obj);
                        hosptis.setViewAdapter(hospWheelAdapter);
                        // results = JSON.parseObject(obj.getString("results").toString(), new TypeReference<List<Region>>() {
                        //  });
                        //  mCommonDao.clearAllRegions();
                        //	Log.i("1222222222",obj.getString("results").toString());
                        //  mCommonDao.batchAddRegions(results);
                        //     convertRegions(results);

                        //      location();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;

                default:
                    break;
            }
        }
    };

        private void getCode() {
            Map<String, Object> param = new HashMap<String, Object>();
            String phone1 = edit_phone.getText().toString().trim();
            if (!phone1.matches("^\\d{11}$")) {
                LayoutInflater inflater = getLayoutInflater();
                //??????XML?????????ID???????????????
                View layout = inflater.inflate(R.layout.shibai,null);

                ImageView image = (ImageView) layout.findViewById(R.id.shibaiimga);
                //???ò??????????????
                image.setImageResource(R.drawable.shibai);



                TextView text = (TextView) layout.findViewById(R.id.shibaitext);
                //????????
                text.setText("????????????????");

                Toast toast= new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER , 0, 0);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);
                toast.show();
//            Toast  toast = Toast.makeText(getApplicationContext(),
//                    "????????????????", Toast.LENGTH_LONG);
//            toast.setGravity(Gravity.CENTER, 0, 0);
//            LinearLayout toastView = (LinearLayout) toast.getView();
//            ImageView imageCodeProject = new ImageView(getApplicationContext());
//            imageCodeProject.setImageResource(R.drawable.shibai);
//            toastView.addView(imageCodeProject, 0);
//            toast.show();
                //   Toast.makeText(getApplicationContext(), "????????????????", Toast.LENGTH_LONG).show();
                return;
            }
            param.put("Mobile", phone1);
            if(!getCodefalg){
                //     Toast.makeText(getApplicationContext(), i+"???????", Toast.LENGTH_LONG).show();
                return;
            }
            //  getCode.setEnabled(false);


              timer time=new timer();
            time.start();


        }
   private class timer extends Thread{
       @Override
       public void run() {
           super.run();
           Message msg = new Message();

           msg.arg1 = 0;

           hanlder1.sendMessage(msg);
       }
   }

    private Handler handler2 = new Handler() {
        public void handleMessage(Message msg) {
            Timer timer = (Timer) msg.obj;
            getCode.setBackgroundResource(R.drawable.register_codebtn_bg_off);
            //   getCode.setEnabled(false);
            getCode.setText(i + "???????");
            if (i <= 1) {
                getCode.setEnabled(true);
                getCodefalg=true;
                getCode.setBackgroundResource(R.drawable.huoquyanzhengma);
                getCode.setText("");
                i = 60;
                timer.cancel();

            }
        }

    };
    private Handler hanlder1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.arg1) {
                case -1:
                    break;
                case 0:
                    JSONObject obj = (JSONObject) msg.obj;
                 //  if (Constant.dealResponse(RegisterPersonActivity.this, obj)) {
                        //getCode.setVisibility(View.GONE);
                        getCodefalg=false;
                        //    getCode.setEnabled(false);
                        final Timer timer = new Timer();
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                i--;
                                Message msg = new Message();
                                msg.obj = timer;
                                handler2.sendMessage(msg);
                            }
                        }, 1000, 1000);
                 //   }
                    break;
                default:
                    break;
            }
        }
    };



    private void sub() {
            String phone = edit_phone.getText().toString().trim();
            String pwd = edit_pwd.getText().toString().trim();
            String code = edit_code.getText().toString().trim();
            String pwd2 = edit_pwd2.getText().toString().trim();
            if (!phone.matches("^\\d{11}$")) {  LayoutInflater inflater = getLayoutInflater();
                //??????XML?????????ID???????????????
                View layout = inflater.inflate(R.layout.shibai,null);

                ImageView image = (ImageView) layout.findViewById(R.id.shibaiimga);
                //???ò??????????????
                image.setImageResource(R.drawable.shibai);



                TextView text = (TextView) layout.findViewById(R.id.shibaitext);
                //????????
                text.setText("?????????");

                Toast toast= new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER , 0, 0);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);
                toast.show();
//            Toast  toast = Toast.makeText(getApplicationContext(),
//                    "?????????", Toast.LENGTH_LONG);
//            toast.setGravity(Gravity.CENTER, 0, 0);
//            LinearLayout toastView = (LinearLayout) toast.getView();
//            ImageView imageCodeProject = new ImageView(getApplicationContext());
//            imageCodeProject.setImageResource(R.drawable.shibai);
//            toastView.addView(imageCodeProject, 0);
//            toast.show();
                //   Toast.makeText(getApplicationContext(), "????????????????", Toast.LENGTH_LONG).show();
                return;

            }
            if ("".equals(code)) {
                LayoutInflater inflater = getLayoutInflater();
                //??????XML?????????ID???????????????
                View layout = inflater.inflate(R.layout.shibai,null);

                ImageView image = (ImageView) layout.findViewById(R.id.shibaiimga);
                //???ò??????????????
                image.setImageResource(R.drawable.shibai);



                TextView text = (TextView) layout.findViewById(R.id.shibaitext);
                //????????
                text.setText("?????????");

                Toast toast= new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER , 0, 0);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);
                toast.show();
//            Toast  toast = Toast.makeText(getApplicationContext(),
//                    "?????????", Toast.LENGTH_LONG);
//            toast.setGravity(Gravity.CENTER, 0, 0);
//            LinearLayout toastView = (LinearLayout) toast.getView();
//            ImageView imageCodeProject = new ImageView(getApplicationContext());
//            imageCodeProject.setImageResource(R.drawable.shibai);
//            toastView.addView(imageCodeProject, 0);
//            toast.show();
                //    Toast.makeText(getApplicationContext(), "??????????", Toast.LENGTH_LONG).show();
                return;
            }
            if (schoolId == 0) {
                LayoutInflater inflater = getLayoutInflater();
                //??????XML?????????ID???????????????
                View layout = inflater.inflate(R.layout.shibai,null);

                ImageView image = (ImageView) layout.findViewById(R.id.shibaiimga);
                //???ò??????????????
                image.setImageResource(R.drawable.shibai);



                TextView text = (TextView) layout.findViewById(R.id.shibaitext);
                //????????
                text.setText("???????");

                Toast toast= new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER , 0, 0);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);
                toast.show();
//            Toast  toast = Toast.makeText(getApplicationContext(),
//                    "??????У", Toast.LENGTH_LONG);
//            toast.setGravity(Gravity.CENTER, 0, 0);
//            LinearLayout toastView = (LinearLayout) toast.getView();
//            ImageView imageCodeProject = new ImageView(getApplicationContext());
//            imageCodeProject.setImageResource(R.drawable.shibai);
//            toastView.addView(imageCodeProject, 0);
//            toast.show();
                //   Toast.makeText(getApplicationContext(), "?????????????", Toast.LENGTH_LONG).show();
                return;

            }

            String reg = "^[\\w]{6,}$";
            if (!pwd.matches(reg)) {
                LayoutInflater inflater = getLayoutInflater();
                //??????XML?????????ID???????????????
                View layout = inflater.inflate(R.layout.shibai,null);

                ImageView image = (ImageView) layout.findViewById(R.id.shibaiimga);
                //???ò??????????????
                image.setImageResource(R.drawable.shibai);



                TextView text = (TextView) layout.findViewById(R.id.shibaitext);
                //????????
                text.setText("??????????");

                Toast toast= new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER , 0, 0);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);
                toast.show();
//            Toast  toast = Toast.makeText(getApplicationContext(),
//                    "??????????", Toast.LENGTH_LONG);
//            toast.setGravity(Gravity.CENTER, 0, 0);
//            LinearLayout toastView = (LinearLayout) toast.getView();
//            ImageView imageCodeProject = new ImageView(getApplicationContext());
//            imageCodeProject.setImageResource(R.drawable.shibai);
//            toastView.addView(imageCodeProject, 0);
//            toast.show();
                //   Toast.makeText(getApplicationContext(), "?????????????", Toast.LENGTH_LONG).show();
                return;
//            Toast.makeText(getApplicationContext(), "??????????????6λ?????????", Toast.LENGTH_LONG).show();
//            return;

            }
            if (!pwd2.equals(pwd)) {
                LayoutInflater inflater = getLayoutInflater();
                //??????XML?????????ID???????????????
                View layout = inflater.inflate(R.layout.shibai,null);

                ImageView image = (ImageView) layout.findViewById(R.id.shibaiimga);
                //???ò??????????????
                image.setImageResource(R.drawable.shibai);
                TextView text = (TextView) layout.findViewById(R.id.shibaitext);
                //????????
                text.setText("???????????????");
                Toast toast= new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER , 0, 0);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);
                toast.show();
//            Toast  toast = Toast.makeText(getApplicationContext(),
//                    "???????????????", Toast.LENGTH_LONG);
//            toast.setGravity(Gravity.CENTER, 0, 0);
//            LinearLayout toastView = (LinearLayout) toast.getView();
//            ImageView imageCodeProject = new ImageView(getApplicationContext());
//            imageCodeProject.setImageResource(R.drawable.shibai);
//            toastView.addView(imageCodeProject, 0);
//            toast.show();
                //   Toast.makeText(getApplicationContext(), "?????????????", Toast.LENGTH_LONG).show();
                return;
//            Toast.makeText(getApplicationContext(), "????????????????", Toast.LENGTH_LONG).show();
//            return;
            }

            if(!agree.isChecked()){
                LayoutInflater inflater = getLayoutInflater();
                //??????XML?????????ID???????????????
                View layout = inflater.inflate(R.layout.shibai,null);

                ImageView image = (ImageView) layout.findViewById(R.id.shibaiimga);
                //???ò??????????????
                image.setImageResource(R.drawable.shibai);



                TextView text = (TextView) layout.findViewById(R.id.shibaitext);
                //????????
                text.setText("?????????????Э?顷");

                Toast toast= new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER , 0, 0);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);
                toast.show();
//            Toast  toast = Toast.makeText(getApplicationContext(),
//                    "?????????????Э?顷", Toast.LENGTH_LONG);
//            toast.setGravity(Gravity.CENTER, 0, 0);
//            LinearLayout toastView = (LinearLayout) toast.getView();
//            ImageView imageCodeProject = new ImageView(getApplicationContext());
//            imageCodeProject.setImageResource(R.drawable.shibai);
//            toastView.addView(imageCodeProject, 0);
//            toast.show();
                //   Toast.makeText(getApplicationContext(), "?????????????", Toast.LENGTH_LONG).show();
                return;
//            Util.showToast(this,"δ????????Э??");
//            return;
            }

            pd = ProgressDialog.show(RegisterActivity.this, "????..",
                    "????..?????....", true, true);

//            Device (!NetUtil.isNetworkAvailable(RegisterPersonActivity.this)) {
//                LayoutInflater inflater = getLayoutInflater();
//                //??????XML?????????ID???????????????
//                View layout = inflater.inflate(R.layout.shibai,null);
//
//                ImageView image = (ImageView) layout.findViewById(R.id.shibaiimga);
//                //???ò??????????????
//                image.setImageResource(R.drawable.shibai);
//
//
//
//                TextView text = (TextView) layout.findViewById(R.id.shibaitext);
//                //????????
//                text.setText("????????????");
//
//                Toast toast= new Toast(getApplicationContext());
//                toast.setGravity(Gravity.CENTER , 0, 0);
//                toast.setDuration(Toast.LENGTH_SHORT);
//                toast.setView(layout);
//                toast.show();
//                //   Toast.makeText(getApplicationContext(), "????????????", Toast.LENGTH_LONG).show();
//                return;
//            }

//            NetUtil.HttpPostData(Url.register_User, param, new HttpAsyncCallback() {
//                @Override
//                public void onSuccess(JSONObject post) {
//                    // TODO Auto-generated method stub
//                    if (post != null) {
//                        try {
//                            if (pd != null) pd.dismiss();
//                            String isNo = post.getString("isNo");
//                            Message msg = new Message();
//                            if ("0".equals(isNo)) {
//                                msg.arg1 = 0;
//                                msg.obj = post;
//                                hanlder1.sendMessage(msg);
//                            } else if ("-1".equals(isNo)) {
//
//                            }
//                        } catch (JSONException e) {
//                            // TODO Auto-generated catch block
//                            e.printStackTrace();
//                        }
//
//                    }
//                }
//            });
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            //????????????????????????????
//            if (Constant.RESPONSED_SCHOOL == resultCode) {
//                String schoolName = data.getExtras().getString("schoolName");
//                schoolId = data.getExtras().getInt("schoolId");
//                tv_name.setText(schoolName);
//            }
 //           super.onActivityResult(requestCode, resultCode, data);
        }

    }
